/*
 * Created on Wed Jun 06 2018 by Ronnie Ren (zhaosong.ren) from Accenture
 *
 * Copyright (c) 2018 DingXing information & technology limited company.
 */

import { Injectable } from "@angular/core";
import { SqliteService } from "../../services/sqliteDB/sqlite.service";
import { UserCharacteristicRelationshipDto } from "../dto/user-characteristic-relationship.dto";
import { UserCharacteristicRelationship } from "../transform/user-characteristic-relationship";
import {JsonMappingUtil} from "../json-mapping-util";
import {Observable} from "rxjs/Observable";
import {SyncStatus} from "../sync-status";
import { CurdService } from "./curd.service";


@Injectable()
export class UserCharacteristicRelationshipService {

    constructor(private sqliteService: SqliteService,
                private curdService: CurdService) {

    }

  fetchUsesBySyncStatus(syncStatus: SyncStatus): Observable<Array<UserCharacteristicRelationship>> {
    let userCharacteristicRelationshipDto = new UserCharacteristicRelationshipDto();

    return Observable.fromPromise(
      this.sqliteService.executeSqlWithResults<UserCharacteristicRelationshipDto>
      (`select * from ${userCharacteristicRelationshipDto.table().tableName} where syncStatus = '${syncStatus}'`, UserCharacteristicRelationshipDto)
    ).map((rows: Array<{}>) => {
      console.log(rows);
      return rows.map((row) => {
        let userCharacteristicRelationship = new UserCharacteristicRelationship({});
        JsonMappingUtil.MappingAllProp(row, userCharacteristicRelationship);
        return userCharacteristicRelationship;
      });
    });
  }

  updateUserCharacteristicRelationUserSyncStatus(filterValue: SyncStatus, syncStatus: SyncStatus) {
      return Observable.fromPromise(
        this.curdService.update({
          dto: UserCharacteristicRelationshipDto,
          mapObj: UserCharacteristicRelationship
        }, {syncStatus: syncStatus} ,
        {and: {syncStatus: filterValue}}
      ));
  }




}
